package com.qf.shixi.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;
import java.util.UUID;

public class SendSms {

    public static String Sms(String tel) {

        //设置访问的短信平台用户信息
        DefaultProfile profile = DefaultProfile.getProfile("cn-shenzhen", "LTAI5t6McjfRcJBVuAuk86xR", "JbSV5YlPEDfDvgIfgYU5nzSXHivqbM");
        //创建一个本地客户端(想短信平台发送请求)
        IAcsClient client = new DefaultAcsClient(profile);
        //创建一个请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers","15892222603");
        request.putQueryParameter("SignName", "阳阳商城");
        request.putQueryParameter("TemplateCode", "SMS_205409285");
        request.putQueryParameter("PhoneNumbers", tel);
        String code = UUID.randomUUID().toString().substring(0,5);
        HashMap<String, String> map = new HashMap<>();
        map.put("code",code);
        String s = JSONObject.toJSONString(map);
        request.putQueryParameter("TemplateParam", s);
        try {
            CommonResponse response = client.getCommonResponse(request);
            String rs = response.getData();
            JSONObject jsonObject = JSONObject.parseObject(rs);
            String result = (String)jsonObject.get("Code");
            if(result.equalsIgnoreCase("OK")){
                // 将验证码返回给调用者
                return code;
            }else{
                return null;
            }
        } catch (ServerException e) {
            e.printStackTrace();
            return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }
}
